<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ParkInfo extends Model
{

    public $table="t_park_info";

    public $connection="config_cloud";

    public $timestamps=false;

    public $primaryKey='park_code';

        public $fillable=[
        'parking_name',
        'space_total',
        'parking_gps',
        'oid',
        'parking_type',
        'sort_by',
        'status',
        'long_park_time',
        'overnight_car',
        'saturation_max',
        'saturation_min',
        'support_payment',
        'type_code'
    ];

    public $hidden = [
        'id',
        'cid',
        'status',
        'show_in_app',
        'arm_version',
        'province_code',
        'city_code',
        'area_code',
        'region_id',
        'park_code_hash',
        'tmp_space_empty',
        'tmp_space_total',
        'parking_address',
        'discount_info',
        'contact_user',
        'contact_mobile',
        'charge_criterion',
        'open_time',
        'cost_type',
        'parking_channels',
        'others',
        'update_time',
        'support_quick_pass',
        'rentable',
        'staggerable',
        'recongnize_rate',
        'save_pic',
        'parking_price',
        'parking_price_noon',
        'reserve_time',
        'show_qr',
        'notice_on',
        'full_address',
        'createon',
        'plain_img',
        'park_id',
        'park_parent',
        'is_inspect',
        'warranty_start_date',
        'warranty_end_date',
        'warranty_end_date',
        'inspect_date',
        'inspect_period',
        'prev_inspected_date',
        'has_stereo_garage',
        'park_type'
    ];

    public function scopeActive($query)
    {
        return $query->where('status',0)->whereNotNull('oid');
    }

    public function organization()
    {
        return $this->belongsTo(Organization::class,'oid');
    }

    public function parkChannels()
    {
        return $this->hasMany(ParkChannels::class, 'park_code');
    }
    public function parkUsers()
    {
        return $this->hasMany(ParkUser::class, 'park_id',"park_code");
    }

    public function setSupportPaymentAttribute($value)
    {
        $payment='0000zw0';

        $payment=str_replace('zw',implode('',$value),$payment);

        $this->attributes['support_payment'] = bindec($payment);
    }

    public function getSupportPaymentAttribute($value)
    {
        $value = decbin($value) | 10000000;

        $value = substr($value,5,2);

        return str_split($value);
    }

    public function setParkingTypeAttribute($value)
    {
       return $this->attributes['type_code']=$value;
    }

    public function getParkingTypeAttribute($value)
    {
        return $this->attributes['type_code'];
    }

    /**
     * 将资源转换成数组。
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray()
    {
        $mergeArray = [
            'organizations'=>[],
        ];

        if(!is_null($this->organization)) {
            $mergeArray['organizations'] = Organization::ancestorsAndSelf($this->oid);
        }

        return array_merge(parent::toArray(), $mergeArray);
    }
}
